package generics;

/**
 * Created with IntelliJ IDEA
 * Description：通配符上界和下界
 * User：chenshu
 * Date：2023-10-26
 * Time：22:23
 */
class Food {

}
class Fruit extends Food {

}
class Meat extends Food {

}
class Apple extends Fruit {

}
class Banana extends Fruit  {

}
class Plate<T> {
    private T food;

    public T getPlate() {
        return food;
    }

    public void setPlate(T food) {
        this.food = food;
    }
}
public class TestDemo5 {

    public static void main(String[] args) {
        Food food = new Food();

        Plate<Fruit> plate1 = new Plate<>();
        fun(plate1);

        Plate<Apple> plate2 = new Plate<>();
        //fun(plate2);这里不行
    }

    //通配符的下界->只能接收Fruit及其父类:适合写数据，不适合读数据
    public static void fun(Plate<? super Fruit> temp) {
        temp.setPlate(new Apple());
        temp.setPlate(new Banana());
        //temp.setPlate(new Food());//出现向下转型的问题：只有原本通过向上转型的对象才能，才能向下转型回原对象

        //Fruit fruit = temp.getPlate();
    }

    public static void main1(String[] args) {
        Plate<Apple> plate1= new Plate<>();
        Plate<Banana> plate2 = new Plate<>();
        Plate<Meat> plate3 = new Plate<>();
        fun1(plate1);
        fun1(plate2);
        //fun(plate3);这里就不可以
    }

    //通配符的上界：只能接收Fruit及其子类，一般用来读取元素
    public static void fun1(Plate<? extends Fruit> temp) {
        System.out.println(temp.getPlate());
        Fruit fruit = temp.getPlate();
    }
}
